package com.huangj.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.huangj.entity.HomeEntity;
import com.huangj.pro2_h2pop.HomeInfoActivity;
import com.huangj.pro2_h2pop.R;

import java.util.ArrayList;
import java.util.List;


public class HomeListViewAdapter extends BaseAdapter implements  AdapterView.OnItemClickListener {

    private Context context;
    private List<HomeEntity.DataBean> datas;
    private HomeGridViewAdapter homeGridViewAdapter;

    public HomeListViewAdapter(Context context) {
        this.context = context;
        this.datas = new ArrayList<>();
    }

    public void setDatas(List<HomeEntity.DataBean> datas){
        this.datas = datas;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView != null){
            viewHolder = (ViewHolder) convertView.getTag();
        }else{
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_homelistview,null);
            viewHolder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
            viewHolder.tv_bottom = (TextView) convertView.findViewById(R.id.tv_bottom);
            viewHolder.gv_home = (GridView) convertView.findViewById(R.id.gv_home);
            convertView.setTag(viewHolder);
        }
        if (datas != null) {
            viewHolder.tv_address.setText(datas.get(position).getName());
            viewHolder.tv_bottom.setText(datas.get(position).getButton_text());
        }
        //GridView;
        homeGridViewAdapter = new HomeGridViewAdapter(context,datas.get(position).getDestinations());
        viewHolder.gv_home.setAdapter(homeGridViewAdapter);
        viewHolder.gv_home.setOnItemClickListener(this);
        return convertView;
    }

    /**
     * GridView的点击事件；
     */

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HomeEntity.DataBean.DestinationsBean  item = (HomeEntity.DataBean.DestinationsBean) parent.getAdapter().getItem(position);
        int infoid = item.getId();
        Intent intent = new Intent(context, HomeInfoActivity.class);
        intent.putExtra("id",infoid);
        context.startActivity(intent);
    }

    class ViewHolder{
        TextView tv_address,tv_bottom;
        GridView gv_home;
    }
}

